using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using ViCode_LeVi.Controls.Logs;
using ViCode_LeVi.Configs;
using ViCode_LeVi.Controls.Charts;
using DevExpress.XtraPrinting;
using ViCode_LeVi.Data;
using ViCode_LeVi.Controls.BTBData;
using System.Windows.Forms;
using System.IO;

namespace ViCode_LeVi.Controls.Reports
{
    public partial class XtraReport_PotentialPortfolioChart : DevExpress.XtraReports.UI.XtraReport
    {
        private bool m_bLandscapeOrientation;
        private RootData m_oWorkingProject;
        private string m_sTitle;
        private static bool s_bExportCVS = false;
        private static bool s_bExportHTM = VMConfig.Intance.ExpertMode.ShowHtmlExportOption;
        private static bool s_bExportMHT = false;
        private static bool s_bExportPDF = true;
        private static bool s_bExportRTF = VMConfig.Intance.ExpertMode.ShowRtfExportOption;
        private static bool s_bExportTXT = false;
        private static bool s_bExportXLS = false;
        private static bool s_bShowSendAsMailExportOption = VMConfig.Intance.ExpertMode.ShowSendAsMailExportOption;
        public static Color s_colBlueDark = Color.FromArgb(0x2b, 0x63, 0xb7);
        public static Color s_colTableHeader = s_colTableHeaderGray;
        public static Color s_colTableHeaderBlue = Color.FromArgb(0xdd, 230, 0xf2);
        public static Color s_colTableHeaderGray = Color.FromArgb(0xf3, 0xf3, 0xf3);
        public static int s_iChartHeight = 0x1a9;
        public static int s_iDetailHeight = 0x1bd;
        public static int s_iLandscapeBerichtWidth = 200;
        public static int s_iLandscapePageWidth = 0x3c9;
        public static string s_sBarChartImportance = "Weightings-Diagram";
        public static string s_sBarChartQQS = "Diagrams";
        public static string s_sImpactNet = "Net effect";
        public static string s_sPortfolioChart = "QQS-Portfolio";
        public static string s_sPotentialChart = "Potenzialdiagramm";
        public static string s_sPotentialPortfolioChart = "Potentially Portfolio";
        public static string s_sProfileChart = "QQS-Profildiagramm";
        public XtraReport_PotentialPortfolioChart()
        {
            this.m_sTitle = "Titel";
            string rsErr = "";
            try
            {
                this.InitializeComponent();
                this.SetHeaderFooter(ref rsErr);
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
            //ShowReportDefault();
            AddNewSubReport(new ViCode_LeVi.Controls.Reports.PotentialPortfolioChartSubReport(), 8, true);
        }
        DevExpress.XtraReports.UI.XRSubreport xrSubreport1;
        PotentialPortfolioChartSubReport potentialPortfolioChartSubReport1;
        void ShowReportDefault()
        {
            //((System.ComponentModel.ISupportInitialize)(this.potentialPortfolioChartSubReport1)).BeginInit();
            //((System.ComponentModel.ISupportInitialize)(this.potentialPortfolioChartSubReport1)).EndInit();
           
            this.xrSubreport1 = new DevExpress.XtraReports.UI.XRSubreport();
            this.potentialPortfolioChartSubReport1 = new ViCode_LeVi.Controls.Reports.PotentialPortfolioChartSubReport();
            // 
            // xrSubreport1
            // 
            this.xrSubreport1.LocationFloat = new DevExpress.Utils.PointFloat(0F, 0F);
            this.xrSubreport1.Name = "xrSubreport1";
            this.xrSubreport1.ReportSource = this.potentialPortfolioChartSubReport1;
            this.xrSubreport1.SizeF = new System.Drawing.SizeF(100F, 23F);

            this.Detail.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] {
            this.xrSubreport1});
        }
        public int AddNewSubReport(object oChartSubReport, int iNumberOfElements, bool bHasInterpretation)
        {
            int num = 0;
            int showEmptyCharts = VMConfig.Intance.Charts.ShowEmptyCharts;
            try
            {
                XtraReport report = oChartSubReport as XtraReport;
                if ((report != null) && ChartReportIsToAdd(iNumberOfElements, bHasInterpretation))
                {
                    this.AddSubReport2().ReportSource = report;
                    num = 1;
                }
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
            return num;
        }
        public DevExpress.XtraReports.UI.XRSubreport AddSubReport2()
        {
            DevExpress.XtraReports.UI.XRSubreport xrSubreport1 = null;
            try
            {
                xrSubreport1 = new DevExpress.XtraReports.UI.XRSubreport()
                {
                    Location = new Point(0, 0)
                };
                xrSubreport1.SizeF = new System.Drawing.SizeF(100F, 23F);
                this.Detail.Controls.Add(xrSubreport1);
                this.Detail.BackColor = Color.Red;
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
            return xrSubreport1;
        }
      

        public static bool ChartReportIsToAdd(int iNumberOfElements, bool bHasInterpretation)
        {
            bool flag = true;
            int showEmptyCharts = VMConfig.Intance.Charts.ShowEmptyCharts;
            if (iNumberOfElements == 0)
            {
                switch (showEmptyCharts)
                {
                    case 0:
                        return false;

                    case 1:
                        if (!bHasInterpretation)
                        {
                            flag = false;
                        }
                        return flag;
                }
            }
            return flag;
        }

        public static void CustomizeUI(XtraReport oXtraReport)
        {
            oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.Watermark, CommandVisibility.None);
        }



        public static int GetFactorTypeGroupIndex(FactorType eFactorType)
        {
            int num = 0;
            try
            {
                FactorType type = eFactorType;
                if (type != FactorType.HK)
                {
                    if (type == FactorType.SK)
                    {
                        return 0x10;
                    }
                    if (type == FactorType.BK)
                    {
                        return 0x20;
                    }
                }
                else
                {
                    return 8;
                }
                num = 0;
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
            return num;
        }        

        public static string GetInterpretationString(string sInterpretation)
        {
            return sInterpretation;
        }

        public static void GetMessageString(System.Collections.Specialized.StringCollection aMessages)
        {
            string str = string.Empty;
            if ((aMessages != null) && VMConfig.Intance.Charts.ShowMissingChartsWarning)
            {
                foreach (string str2 in aMessages)
                {
                    str = str + "\n" + str2;
                }
                if (str != string.Empty)
                {
                    MessageBox.Show("Folgende Bestandteile werden aufgrund fehlender Daten\nnicht dargestellt: \n" + str + ".", "Reports", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }

        public static PeriodList GetTimeLinePeriods(ProjectData oWorkingProject, int iPeriodStartIndex, int iPeriodEndIndex)
        {
            PeriodList list = new PeriodList();
            //todo
            //Period period = null;
            //try
            //{
            //    if ((iPeriodEndIndex > (oWorkingProject.Periods.Count - 1)) || (iPeriodEndIndex < 0))
            //    {
            //        iPeriodEndIndex = oWorkingProject.Periods.Count - 1;
            //    }
            //    if ((iPeriodStartIndex > iPeriodEndIndex) || (iPeriodStartIndex < 0))
            //    {
            //        if ((iPeriodEndIndex - iPeriodStartIndex) > 5)
            //        {
            //            iPeriodStartIndex = iPeriodEndIndex - 5;
            //        }
            //        else
            //        {
            //            iPeriodStartIndex = iPeriodEndIndex;
            //        }
            //    }
            //    for (int i = iPeriodStartIndex; i <= iPeriodEndIndex; i++)
            //    {
            //        if (i >= 0)
            //        {
            //            period = (Period)oWorkingProject.Periods[i];
            //            list.Add(period);
            //        }
            //    }
            //    if (oWorkingProject.Periods.Count > 0)
            //    {
            //        period = (Period)oWorkingProject.Periods[oWorkingProject.Periods.Count - 1];
            //        list.Add(period);
            //    }
            //}
            //catch (Exception exception)
            //{
            //    Log.LogException(exception);
            //}
            return list;
        }



        private bool SetHeaderFooter(ref string rsErr)
        {
            bool flag = true;
            try
            {
                base.Name = this.Title;
                this.cellCompany.Text = ProjectData.Intance.CompanyName;
                this.cellProject.Text = ProjectData.Intance.Name;
                //Todo
                //if (RootData.Intance.CurrentPeriod != null)
                //{
                //    this.lblPeriode.Text = string.Format("{0} ({1} - {2})", RootData.Intance.CurrentPeriod.Name, RootData.Intance.CurrentPeriod.StartDate.ToShortDateString(), RootData.Intance.CurrentPeriod.EndDate.ToShortDateString());
                //}
                this.cellFileName.Text = "FileName: " + ProjectData.Intance.FileName;
                this.cellCreator.Text = "Create By: " + ProjectData.Intance.Author;
            }
            catch (Exception exception)
            {
                flag = false;
                rsErr = "Fehler in ReportTemplate.InitReport: " + exception.Message;
                Log.LogException(exception);
            }
            return flag;
        }

        public static void ShowExportFilters(XtraReport oXtraReport)
        {
            try
            {
                if (s_bExportCVS)
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportCsv, CommandVisibility.All);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendCsv, CommandVisibility.All);
                }
                else
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportCsv, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendCsv, CommandVisibility.None);
                }
                if (s_bExportMHT)
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportMht, CommandVisibility.All);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendMht, CommandVisibility.All);
                }
                else
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportMht, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendMht, CommandVisibility.None);
                }
                if (s_bExportTXT)
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportTxt, CommandVisibility.All);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendTxt, CommandVisibility.All);
                }
                else
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportTxt, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendTxt, CommandVisibility.None);
                }
                if (s_bExportXLS)
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportXls, CommandVisibility.All);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendXls, CommandVisibility.All);
                }
                else
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportXls, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendXls, CommandVisibility.None);
                }
                if (s_bExportHTM)
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportHtm, CommandVisibility.All);
                }
                else
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportHtm, CommandVisibility.None);
                }
                if (s_bExportRTF)
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportRtf, CommandVisibility.All);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendRtf, CommandVisibility.All);
                }
                else
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportRtf, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendRtf, CommandVisibility.None);
                }
                if (s_bExportPDF)
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportPdf, CommandVisibility.All);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendPdf, CommandVisibility.All);
                }
                else
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ExportPdf, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendPdf, CommandVisibility.None);
                }
                if (!s_bShowSendAsMailExportOption)
                {
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendCsv, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendFile, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendGraphic, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendMht, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendPdf, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendRtf, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendTxt, CommandVisibility.None);
                    oXtraReport.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.SendXls, CommandVisibility.None);
                }
            }
            catch (Exception exception)
            {
                Log.LogException(exception);
            }
        }

        public static void ShowReportPreview(object oReportObject)
        {
            try
            {
                XtraReport report = oReportObject as XtraReport;
                if (report != null)
                {
                    if (VMConfig.Intance.Charts.AllowMultiplePreviews)
                    {
                        report.CreateDocument();
                        report.ShowPreview();
                        if (VMConfig.Intance.Charts.MaximiseReports)
                        {
                            //report.PrintingSystem.PreviewForm.WindowState = FormWindowState.Maximized;
                        }
                        else
                        {
                            //report.PrintingSystem.PreviewForm.WindowState = FormWindowState.Normal;
                        }
                    }
                    else
                    {
                        report.ShowPreviewDialog();
                        report.ClosePreview();
                        report.Dispose();
                        report = null;
                    }
                }
            }
            catch (IOException exception)
            {
                MessageBox.Show("Beim Speichern ist folgender Fehler aufgetreten: \n\n " + exception.Message, "Reports", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Log.LogException(exception);
            }
            catch (Exception exception2)
            {
                MessageBox.Show("Bei der Druckvorschau ist folgender Fehler aufgetreten: \n\n " + exception2.Message, "Reports", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Log.LogException(exception2);
            }
        }

        public static bool StringHasValue(string sInput)
        {
            bool flag = true;
            return (((sInput != null) && !(sInput == string.Empty)) && flag);
        }

        public bool LandscapeOrientation
        {
            get
            {
                return this.m_bLandscapeOrientation;
            }
            set
            {
                this.m_bLandscapeOrientation = value;
                base.Landscape = true;
                base.Landscape = this.m_bLandscapeOrientation;
                int num = s_iLandscapePageWidth;
                this.xrTable1.Width = num;
                this.xrTable2.Width = num;
                this.cellBericht.Width = s_iLandscapeBerichtWidth;
                this.cellCompanyLbl.Width = s_iLandscapeBerichtWidth;
                this.cellProjectLbl.Width = s_iLandscapeBerichtWidth;
                this.lblPeriode.Width = num;
                this.pageInfoDate.Width = num;
            }
        }

        public string Title
        {
            get
            {
                return this.m_sTitle;
            }
            set
            {
                this.m_sTitle = value;
                this.cellTitel.Text = this.m_sTitle;
            }
        }        
    }
}
